﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TugasAkhir.Models;
using TugasAkhir.Objects;

namespace TugasAkhir
{
    public partial class PengaturanRekomendasi : System.Web.UI.Page
    {
        private TanggalObject tanggal;
        private string tahunSekarang;
        private string periodeSekarang;

        private DosenObject dosen;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckSession();
            CheckRole();

            TanggalModel.GetServerWaktu(out tanggal);
            this.Cek();

            if (!Page.IsPostBack)
            {
                this.InitMatakuliah();
            }
        }

        private void InitMatakuliah()
        {
            var defaultItem = new ListItem()
            {
                Text = "-- pilih salah satu --",
                Value = ""
            };

            this.ddlMatakuliah.Items.Clear();
            this.ddlMatakuliah.Items.Add(defaultItem);

            var hasilQuery = new DataSet();
            PenggunaModel.GetIdLab(this.dosen, out hasilQuery);

            string idLab = hasilQuery.Tables[0].Rows[0][0].ToString();

            var data = new DataSet();
            bool flag = DetailPertanyaanModel.GetMatakuliah(this.tahunSekarang, this.periodeSekarang, idLab, out data);

            if (flag)
            {
                foreach (DataRow item in data.Tables[0].Rows)
                {
                    this.ddlMatakuliah.Items.Add(new ListItem { Text = item[1].ToString(), Value = item[0].ToString() });
                }
            }

            if (this.ddlPertanyaan.Items.Count == 0)
                this.ddlPertanyaan.Items.Add(defaultItem);
        }

        private void CheckRole()
        {
            if (int.Parse(Session["id_role"].ToString().Substring(2)) == 2)
            {
                this.liPertanyaan.Attributes.Remove("hidden");
                this.liPengaturanPertanyaan.Attributes.Remove("hidden");
                this.liAnalisis.Attributes.Remove("hidden");
                this.liRekomendasi.Attributes.Remove("hidden");
            }
            else if (int.Parse(Session["id_role"].ToString().Substring(2)) == 3)
            {
                this.liAnalisis.Attributes.Remove("hidden");
                this.liRekomendasi.Attributes.Remove("hidden");
            }
            else
            {
                this.liRekomendasi.Attributes.Remove("hidden");
                this.liAnalisis.Attributes.Remove("hidden");
                this.liPengaturanRekomendasi.Attributes.Remove("hidden");
                this.liDaftarRekomendasi.Attributes.Remove("hidden");
            }
        }

        private void CheckSession()
        {
            if (Session["id_role"] != null && Session["id_role"].ToString() != "" && int.Parse(Session["id_role"].ToString().Substring(2)) > 3)
            {
                DosenModel.GetDosenDetail(Session["username"].ToString(), out this.dosen);
                this.litName.Text = dosen.NamaDosen;
            }
            else
            {
                Response.Redirect("/Login.aspx");
            }
        }

        private void Cek()
        {
            this.periodeSekarang = (this.tanggal.Bulan >= 1 && this.tanggal.Bulan <= 6) ? "GENAP" : "GASAL";
            this.tahunSekarang = String.Format("{0}-{1}", this.tanggal.Tahun, this.tanggal.Tahun + 1);

            this.litKeterangan.Text = String.Format("Tahun Ajaran: {0} Periode: {1}", this.tahunSekarang, this.periodeSekarang);
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Session["id_role"] = "";
            Session["username"] = "";
            CheckSession();
        }

        protected void ddlMatakuliah_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.hfMatakuliah.Value = this.ddlMatakuliah.SelectedValue;
            this.hfPertanyaan.Value = "";

            if (this.ddlMatakuliah.SelectedValue == "")
            {
                var defaultItem = new ListItem()
                {
                    Text = "-- pilih salah satu --",
                    Value = ""
                };

                this.ddlPertanyaan.Items.Clear();
                this.ddlPertanyaan.Items.Add(defaultItem);
            }
            else
            {
                this.InitPertanyaan();
            }

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowTabel", "Init();", true);
        }

        private void InitPertanyaan()
        {
            var defaultItem = new ListItem()
            {
                Text = "-- pilih salah satu --",
                Value = ""
            };

            this.ddlPertanyaan.Items.Clear();
            this.ddlPertanyaan.Items.Add(defaultItem);

            var data = new DataSet();
            bool flag = DetailPertanyaanModel.GetPertanyaan(this.tahunSekarang, this.periodeSekarang, this.hfMatakuliah.Value, out data);

            if (flag)
            {
                foreach (DataRow item in data.Tables[0].Rows)
                {
                    this.ddlPertanyaan.Items.Add(new ListItem { Text = item[1].ToString(), Value = item[0].ToString() });
                }
            }
            else
            {
                this.ddlPertanyaan.SelectedValue = "";
            }
        }

        protected void ddlPertanyaan_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.hfPertanyaan.Value = this.ddlPertanyaan.SelectedValue;

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowTabel", "Init();", true);
        }

        protected void btPilih_Click(object sender, EventArgs e)
        {
            InitTabel();
        }

        private void InitTabel()
        {
            if (this.ddlMatakuliah.SelectedValue != "" || this.ddlPertanyaan.SelectedValue != "")
            {
                var hasilQuery = new DataSet();
                if (AnalisisModel.GetRekomendasi(this.hfPertanyaan.Value, this.tahunSekarang, this.periodeSekarang, this.hfMatakuliah.Value, out hasilQuery))
                {
                    this.gvRekomendasi.DataSource = hasilQuery.Tables[0];
                    this.gvRekomendasi.DataBind();

                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowTabel", "Init();", true);

                    this.btTambah.Visible = true;
                }
            }
        }

        protected void btAddRekomendasi_Click(object sender, EventArgs e)
        {
            if (this.ddlRekomendasi.SelectedValue != "")
            {
                var analisis = new AnalisisObject();
                analisis.Periode = this.periodeSekarang;
                analisis.Tahun = this.tahunSekarang;
                analisis.Matakuliah = this.hfMatakuliah.Value;
                analisis.Rekomendasi = this.ddlRekomendasi.SelectedValue;
                analisis.Pertanyaan = this.hfPertanyaan.Value;
                analisis.Validitas = 0.0;
                analisis.Reliabilitas = 0.0;
                analisis.Pengaruh = 0.0;
                analisis.Skor = 0.0;

                if (AnalisisModel.Tambah(analisis))
                {
                    InitTabel();
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowAdd", "$('#addModal').modal('hide');", true);
                }
            }
        }

        protected void btTambah_Click(object sender, EventArgs e)
        {
            this.InitTabel();
            this.InitRekomendasi();

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowAdd", "$('#addModal').modal('show');", true);
        }

        private void InitRekomendasi()
        {
            this.ddlRekomendasi.Items.Clear();

            var data = new DataSet();
            bool flag = RekomendasiModel.GetRekomendasi(this.hfPertanyaan.Value, this.tahunSekarang, this.periodeSekarang, this.hfMatakuliah.Value, out data);

            var defaultItem = new ListItem()
            {
                Text = "-- pilih salah satu --",
                Value = ""
            };

            this.ddlRekomendasi.Items.Clear();
            this.ddlRekomendasi.Items.Add(defaultItem);

            if (flag)
            {
                foreach (DataRow item in data.Tables[0].Rows)
                {
                    this.ddlRekomendasi.Items.Add(new ListItem { Text = item[1].ToString(), Value = item[0].ToString() });
                }
            }
        }

        protected void gvRekomendasi_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("deleteRecord"))
            {
                int index = Convert.ToInt32(e.CommandArgument);
                this.DeleteCommand(index);
            }
        }

        private void DeleteCommand(int index)
        {
            string idRekomendasi = this.gvRekomendasi.DataKeys[index].Value.ToString();

            this.hfDeleteID.Value = idRekomendasi;

            this.InitTabel();

            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowDelete", "$('#deleteModal').modal('show');", true);
        }

        protected void btDeleteRekomendasi_Click(object sender, EventArgs e)
        {
            var analisis = new AnalisisObject();
            analisis.Periode = this.periodeSekarang;
            analisis.Tahun = this.tahunSekarang;
            analisis.Matakuliah = this.hfMatakuliah.Value;
            analisis.Rekomendasi = this.hfDeleteID.Value;
            analisis.Pertanyaan = this.hfPertanyaan.Value;

            if (AnalisisModel.Hapus(analisis))
            {
                this.InitTabel();

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowDelete", "$('#deleteModal').modal('hide');", true);
            }
        }
    }
}